package com.zjw.common.exception.response;

import com.alibaba.fastjson.JSON;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;


public class ResponseUtil {

    /**
     * 强调会话失效
     * @return
     */
    public static boolean invalidAccess(HttpServletResponse response) throws IOException {
        ResponseVO responseVO = new ResponseVO();
   //     responseVO.setRequestId(GlobeContext.getRequestId());
        responseVO.setRet("-99");
        responseVO.setRetmsg("会话失效，请重新登陆");
        ResponseUtil.writeResponseData(response, responseVO);
        return false;
    }



    public static ResponseVO validateFailure(){
        ResponseVO responseVO = new ResponseVO();
        responseVO.setRet("10220001");
        responseVO.setRetmsg("必填参数不能为空");
    //    responseVO.setRequestId(GlobeContext.getRequestId());
        return responseVO;
    }

    public static ResponseVO error(){
        ResponseVO responseVO = new ResponseVO();
        responseVO.setRet("-1");
        responseVO.setRetmsg("system error");
   //     responseVO.setRequestId(GlobeContext.getRequestId());
        return responseVO;
    }

    public static ResponseVO error(String code, String msg){
        ResponseVO responseVO = new ResponseVO();
        responseVO.setRet(code);
        responseVO.setRetmsg(msg);
    //    responseVO.setRequestId(GlobeContext.getRequestId());
        return responseVO;
    }

    public static boolean error(HttpServletResponse response) throws IOException {
        ResponseVO responseVO = error("-1", "error");
        ResponseUtil.writeResponseData(response, responseVO);
        return false;
    }

    public static ResponseVO match(ResponseEnum enums) {
        ResponseVO responseVO = new ResponseVO();
        responseVO.setRet(enums.val());
        responseVO.setRetmsg(enums.msg());
    //    responseVO.setRequestId(GlobeContext.getRequestId());
        return responseVO;
    }

    public static ResponseVO success(){
        ResponseVO responseVO = new ResponseVO();
        responseVO.setRet("0");
        responseVO.setRetmsg("ok");
        return responseVO;
    }

    public static ResponseVO success(Object o){
        ResponseVO responseVO = success();
        responseVO.setRetdata(o);
        return responseVO;
    }

    public static ResponseVO success(String datakey, Object datavalue){
        HashMap<String, Object> data = new HashMap<>();
        data.put(datakey, datavalue);
        return success(data);
    }


    public static ResponseVO fail(String code, String msg, Object data){
        ResponseVO responseVO = new ResponseVO();
        responseVO.setRet(code);
        responseVO.setRetmsg(msg);
        responseVO.setRetdata(data);
        return responseVO;
    }


    public static void writeResponseData(HttpServletResponse response, ResponseVO vo) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try (PrintWriter out = response.getWriter();){
            out.append(JSON.toJSONString(vo));
            out.flush();
        }
        catch (IOException e){
            e.printStackTrace();
            response.sendError(500);
        }
    }

    public static ResponseVO response(String code, String msg){
        ResponseVO responseVO = new ResponseVO();
        responseVO.setRet(code);
        responseVO.setRetmsg(msg);
        return responseVO;
    }
}
